﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ToaThuoc_PAK
{
    public partial class TraCuuICD10 : System.Web.UI.Page
    {
        string tmabn, tmaicd;
        private DAL_ICD10 dal_ICD10;
        protected void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            dal_ICD10 = new DAL_ICD10();
            tmabn = mabn.Text;
            tmaicd = txtMaICD.Text;
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.danhsach.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.danhsach_PageIndexChanged);
            this.danhsach.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.danhsach_CancelCommand);
            this.danhsach.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.danhsach_EditCommand);
            this.danhsach.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.danhsach_UpdateCommand);
            this.danhsach.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.danhsach_DeleteCommand);

        }
        #endregion

        private void BindData()
        {
            DataTable dt = dal_ICD10.TraCuuICD10(tmabn, tmaicd);
            //phongkhamlogin pklog=new phongkhamlogin();
            //SqlConnection sqlcon=new SqlConnection(pklog.ConnectionString);
            //SqlCommand sqlcom=new SqlCommand();
            //sqlcom.Connection=sqlcon;
            //string sqlselect="";
            //if(mabn.Text!="")
            //    sqlselect="select VVIET,CICD10,vanh from ICD10 where VVIET like N'%"+tmabn+"%' order by CICD10 ";
            //else if(txtMaICD.Text!="")
            //    sqlselect="select VVIET,CICD10,vanh from ICD10 where cicd10 like '%"+tmaicd+"%' order by CICD10 ";
            //else 
            //    sqlselect="select VVIET,CICD10,vanh from ICD10 order by CICD10 ";
            //sqlcon.Open();
            //sqlcom.CommandText=sqlselect;
            //DataSet ds=new DataSet();
            //SqlDataAdapter dbadapter=new SqlDataAdapter(sqlselect,sqlcon);

            //dbadapter.Fill(ds,"ICD10");
            danhsach.DataSource = dt;
            danhsach.DataBind();
            //sqlcon.Close();

        }
        protected void traCuuButton_Click(object sender, System.EventArgs e)
        {
            danhsach.CurrentPageIndex = 0;
            tmabn = mabn.Text;
            BindData();
        }

        protected void danhsach_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            //string a=danhsach.SelectedItem.Cells
            string tenchandoan = ((Label)(danhsach.SelectedItem.Cells[3].Controls[1])).Text.Trim();
            string machandoan = ((Label)(danhsach.SelectedItem.Cells[4].Controls[1])).Text.Trim();
            //string tenchandoan="aaa";
            //string machandoan="ttt";
            lenhDienThongTin.Text = "\n<script language=javascript>" +
                "\n<!--" +
                "\n		dienThongTin('" + tenchandoan + "','" + machandoan + "')" +
                //"\n		dienThongTin('abc','bca')"+
                "\n		" +
                "\n//-->" +
                "\n</script>";
        }

        private void danhsach_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < danhsach.PageCount)
            {
                danhsach.CurrentPageIndex = e.NewPageIndex;
                BindData();
            }
            else
            {
                danhsach.CurrentPageIndex = 0;
            }
        }

        private void danhsach_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            //			mabn.Text=e.Item.Cells[2].Text.Trim();
            //			txtMaICD.Text=e.Item.Cells[3].Text.Trim();
            //			ViewState["CapNhat"]="true";
            //			Button2.Text="Cập nhật";
            danhsach.EditItemIndex = e.Item.ItemIndex;
            BindData();
        }
        private void Button1_Click(object sender, System.EventArgs e)
        {
            if (ViewState["CapNhat"].ToString() == "true")
            {
                dal_ICD10.CapNhatICD10(tmabn, tmaicd);
                //login log=new login();
                //log.Database=log.PhongKhamDB;
                //SqlConnection conn=new SqlConnection(log.ConnectionString);
                //conn.Open();
                //string str="update icd10 set VVIET=N'"+tmabn+"' from ICD10 where cicd10 = '"+tmaicd+"'";
                //SqlCommand comd=new SqlCommand(str, conn);
                //comd.ExecuteNonQuery();
                //conn.Close();
            }
            else
            {
                dal_ICD10.ThemICD10(tmabn, tmaicd);
                //login log=new login();
                //log.Database=log.PhongKhamDB;
                //SqlConnection conn=new SqlConnection(log.ConnectionString);
                //conn.Open();
                //string str="insert into icd10(vviet, cicd10) values(N'"+tmabn+"',N'"+tmaicd+"')";
                //SqlCommand comd=new SqlCommand(str, conn);
                //comd.ExecuteNonQuery();
                //conn.Close();
            }
        }

        protected void Button2_Click(object sender, System.EventArgs e)
        {
            //mabn.Text="";
            //txtMaICD.Text="";
            ViewState["CapNhat"] = "false";
            //Button2.Text="Lưu mới";

            if (ViewState["CapNhat"].ToString() == "true")
            {
                dal_ICD10.CapNhatICD10(tmabn, tmaicd);
                //login log=new login();
                //log.Database=log.PhongKhamDB;
                //SqlConnection conn=new SqlConnection(log.ConnectionString);
                //conn.Open();
                //string str="update icd10 set VVIET=N'"+tmabn+"' from ICD10 where cicd10 = '"+tmaicd+"'";
                //SqlCommand comd=new SqlCommand(str, conn);
                //comd.ExecuteNonQuery();
                //conn.Close();
            }
            else
            {
                dal_ICD10.ThemICD10(tmabn, tmaicd);
                //login log=new login();
                //log.Database=log.PhongKhamDB;
                //SqlConnection conn=new SqlConnection(log.ConnectionString);
                //conn.Open();
                //string str="insert into icd10(vviet, cicd10) values(N'"+tmabn+"',N'"+tmaicd+"')";
                //SqlCommand comd=new SqlCommand(str, conn);
                //comd.ExecuteNonQuery();
                //conn.Close();
            }
            BindData();
        }

        private void danhsach_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            ltrLenh.Text = "<script language=javascript>confirmation('" + e.Item.Cells[4].Text + "','" + e.Item.Cells[3].Text + "');</script>";

        }

        protected void btnXoa_Click(object sender, System.EventArgs e)
        {
            //login log=new login();
            //log.Database=log.PhongKhamDB;
            //SqlConnection conn=new SqlConnection(log.ConnectionString);
            //conn.Open();
            //string str="delete from icd10 where cicd10=N'"+txtMaICD.Text.Trim()+"'";
            //SqlCommand comd=new SqlCommand(str, conn);
            //comd.ExecuteNonQuery();
            //conn.Close();
            dal_ICD10.XoaICD10(txtMaICD.Text.Trim());
            BindData();
        }

        private void danhsach_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            danhsach.EditItemIndex = -1;
            BindData();
        }

        private void danhsach_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            string ten = ((TextBox)e.Item.Cells[3].Controls[1]).Text;
            string ma = ((TextBox)e.Item.Cells[4].Controls[1]).Text;
            dal_ICD10.CapNhatICD10(ten, ma);
            //login log=new login();
            //log.Database=log.PhongKhamDB;
            //SqlConnection conn=new SqlConnection(log.ConnectionString);
            //conn.Open();
            //string str="update icd10 set vviet=N'"+ten+"' where cicd10=N'"+ma+"'";
            //SqlCommand comd=new SqlCommand(str, conn);
            //comd.ExecuteNonQuery();
            //conn.Close();
            danhsach.EditItemIndex = -1;
            BindData();
        }


    }
}




    
